<?php
/**
 * @author kevin
 * @date 2016-03-13 14:00:00
 * @desc 订单管理
 */
 /*
+----------------------------------------------------------------------------------------+
|     订单状态   | 未确认 | 取  消 | 确  认 | 已付款 | 配货中 | 已发货 | 已收货 | 退  货 |
+----------------------------------------------------------------------------------------+
|     App状态    |        | 取  消 | 待付款 | 待发货 | 待发货 | 待收货 | 待评价 | 退  货 |
+----------------------------------------------------------------------------------------+
|order_status	 |   0    |   2    |   1	|   1	 |    1	  |    5   |   5	|    4   |
|shipping_status |   0    |   0    |   0	|   0	 |    3	  |    1   |   2	|    0   |
|pay_status 	 |   0    |   0    |   0	|   2	 |    2	  |    2   |   2	|    0   |
+----------------------------------------------------------------------------------------+
0待付款 1已取消 2待发货 3待收货 4待评价 5已完成 6退货
*/

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}
$version = '1.0';   //版本号

if ($_POST['api_version'] != $version)      //网店的接口版本低
{
	api_err('0x008', 'a low version api');
}
if(empty($_POST['st'])){
	api_err('0x008', 'param st is Missing');
}
//用户ID检测
if(!isset($GLOBALS['uid'])){
	api_err('0x015', 'You have not logged on');
}
$user_id = $GLOBALS['uid'];
//全部订单
if($_POST['st'] == 'list'){
	$size = abs(intval($_POST['size']));
	$size = ($size > 0 && $size < SIZE_MAX)?$size:SIZE_VALUE;
	$page = abs(intval($_POST['page']));
	$page = empty($page)?1:$page;
    $record_count = $db->getOne("SELECT COUNT(*) FROM " .$ecs->table('order_info'). " WHERE user_id = '$user_id' and delete_flag = 0 ");
    $pager  = get_pager('user.php', array('act' => $action), $record_count, $page, $size);
    $result = get_user_ordersv1($user_id, $pager['size'], $pager['start']);
	$data['list'] = get_order_data_list($result);
	$data['page'] = get_app_pager($pager);
}
//待付款
if($_POST['st'] == 'pending'){
	$size = abs(intval($_POST['size']));
	$size = ($size > 0 && $size < SIZE_MAX)?$size:SIZE_VALUE;
	$page = abs(intval($_POST['page']));
	$page = empty($page)?1:$page;
    $record_count = $db->getOne("SELECT COUNT(*) FROM " .$ecs->table('order_info'). " WHERE user_id = '$user_id' and CONCAT(order_status, shipping_status, pay_status) in ('100','000') and delete_flag = 0 ");
    $pager  = get_pager('user.php', array('act' => $action), $record_count, $page, $size);
    $result = get_user_ordersv1($user_id, $pager['size'], $pager['start'],array('100','000'));
	$data['list'] = get_order_data_list($result);
	$data['page'] = get_app_pager($pager);
}
//待发货
if($_POST['st'] == 'preship'){
	$size = abs(intval($_POST['size']));
	$size = ($size > 0 && $size < SIZE_MAX)?$size:SIZE_VALUE;
	$page = abs(intval($_POST['page']));
	$page = empty($page)?1:$page;
    $record_count = $db->getOne("SELECT COUNT(*) FROM " .$ecs->table('order_info'). " WHERE user_id = '$user_id' and CONCAT(order_status, shipping_status, pay_status) in ('102','132') and delete_flag = 0 ");
    $pager  = get_pager('user.php', array('act' => $action), $record_count, $page, $size);
    $result = get_user_ordersv1($user_id, $pager['size'], $pager['start'],array('102','132'));
	$data['list'] = get_order_data_list($result);
	$data['page'] = get_app_pager($pager);
}
//待收货
if($_POST['st'] == 'shipped'){
	$size = abs(intval($_POST['size']));
	$size = ($size > 0 && $size < SIZE_MAX)?$size:SIZE_VALUE;
	$page = abs(intval($_POST['page']));
	$page = empty($page)?1:$page;
    $record_count = $db->getOne("SELECT COUNT(*) FROM " .$ecs->table('order_info'). " WHERE user_id = '$user_id' and CONCAT(order_status, shipping_status, pay_status) in ('512') and delete_flag = 0 ");
    $pager  = get_pager('user.php', array('act' => $action), $record_count, $page, $size);
    $result = get_user_ordersv1($user_id, $pager['size'], $pager['start'],array('512'));
	$data['list'] = get_order_data_list($result);
	$data['page'] = get_app_pager($pager);
}
//待评价
if($_POST['st'] == 'evaluate'){
	$size = abs(intval($_POST['size']));
	$size = ($size > 0 && $size < SIZE_MAX)?$size:SIZE_VALUE;
	$page = abs(intval($_POST['page']));
	$page = empty($page)?1:$page;
    $record_count = $db->getOne("SELECT COUNT(*) FROM " .$ecs->table('order_info'). " WHERE user_id = '$user_id' and CONCAT(order_status, shipping_status, pay_status) in ('522') and delete_flag = 0 and comment_flag = 0");
    $pager  = get_pager('user.php', array('act' => $action), $record_count, $page, $size);
    $result = get_user_ordersv1($user_id, $pager['size'], $pager['start'],array('522'));
	$data['list'] = get_order_data_list($result);
	$data['page'] = get_app_pager($pager);
}
//订单详情
if($_POST['st'] == 'detail'){
	$order_id = isset($_POST['order_id']) ? abs(intval($_POST['order_id'])) : 0;
	if(empty($order_id)){
		api_err('0x008', 'param order_id can not empty');
	}
	$order = get_order_detailv1($order_id, $user_id);
	/* 订单商品 */
	$goods_list = order_goodsv1($order_id);
	foreach($goods_list as $value){
		$list[] = array(
			'goods_id'=>$value['goods_id'],
			'goods_sn'=>$value['goods_sn'],
			'goods_name'=>$value['goods_name'],
			'goods_thumb'=>get_image_pathv1('', $value['goods_thumb']),
			'market_price'=>$value['market_price'],
			'goods_price'=>$value['goods_price'],
			'goods_number'=>$value['goods_number'],
			'subtotal'=>$value['subtotal'],
			'is_gift'=>$value['is_gift']
		);
	}
	//订单信息
	$data['order_info'] = array(
		'order_id'=>$order['order_id'],
		'order_sn'=>$order['order_sn'],
        'original_price'=>$order['original_amount'],
		'goods_amount'=>$order['goods_amount'],
        'total_fee'=>$order['total_fee'],
        'reduce'=>$order['original_amount'] - $order['goods_amount'],
		'discount'=>$order['discount'],
		'status'=>$order['status'],
		'shipping_fee'=>$order['shipping_fee'],
		'add_time'=>local_date($GLOBALS['_CFG']['time_format'], $order['add_time']),
		'postscript'=>$order['postscript']
	);
    $data['order_info']['reduce'] = strval($data['order_info']['reduce']>0?$data['order_info']['reduce']:'0.00');
	$province = get_current_name(1, $order['province']);
	$city = get_current_name(2, $order['city']);
	$district = get_current_name(3, $order['district']);
	 //送货信息
	$data['member_info'] = array(
		'consignee'=>$order['consignee'],
		'tel'=>$order['tel'],
		'address'=>"{$province['region_name']}{$city['region_name']}{$district['region_name']}{$order['address']}"
	);
	//支付信息
	if(!empty($order['pay_time_time'])){
		$data['pay_info'] = array(
			array(
				'pay_name'=>empty($order['pay_name'])?"在线支付":$order['pay_name'],
				'pay_time'=>local_date($GLOBALS['_CFG']['time_format'], strtotime($order['pay_time'])),
			)
		);
	}
	//物流信息
	if(!empty($order['shipping_name'])){
		$data['ship_info'] = array(
			'shipping_id'=>$order['shipping_id'],
			'shipping_name'=>$order['shipping_name'],
			'invoice_no'=>$order['invoice_no']
		);
	}
	//订单商品信息
	$data['goods_list'] = $list;
}
//删除订单
if($_POST['st'] == 'delete'){
	$order_id = abs(intval($_POST['order_id']));
	if(empty($order_id)){
		api_err('0x008', 'param order_id can not empty');
	}
	$sql = "update ".$ecs->table('order_info')." set delete_flag = 1  WHERE order_id='$order_id'";
	if(!$GLOBALS['db'] ->query($sql)){
		api_err('0x008', 'delete order is failed');
	}
	$data = array();
}
//取消订单
if($_POST['st'] == 'cancel'){
	$order_id = isset($_POST['order_id']) ? abs(intval($_POST['order_id'])) : 0;
	if(empty($order_id)){
		api_err('0x008', 'param order_id can not empty');
	}
    if (!cancel_order($order_id, $user_id)){
        api_err('0x008', 'Order cancellation failed');
    }
	$data = array();
}
//st参数错误
if(!isset($data)){
	api_err('0x008');
}
data_back($data, '', RETURN_TYPE);  //返回数据

function get_order_data_list(array $param){
	foreach($param as $value){
		$order = get_order_detailv1($value['order_id'], $user_id);
		/* 订单商品 */
		$goods_list = order_goodsv1($value['order_id']);
		$goods_number = 0;
		$goods_thumb = array();
		foreach ($goods_list AS $key => $v)
		{
            if($v['is_gift'] == 0){
			    $goods_number += $v['goods_number'];
            }
			$goods_thumb[] = get_image_pathv1('', $v['goods_thumb']);
		}
		$res[] = array(
			'order_id'=>$order['order_id'],
			'order_sn'=>$order['order_sn'],
			'status'=>$value['status'],
			'order_status'=>$order['order_status'],
			'pay_status'=>$order['pay_status'],
			'shipping_status'=>$order['shipping_status'],
			'goods_amount'=>$order['total_fee'],
			'goods_number'=>$goods_number,
			'order_time'=>$value['order_time'],
			'goods_thumb'=>$goods_thumb
		);
	}
	return $res;
}
